home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / inetutil.1 / inetutil / inetutils-1.1 / libtelnet / encrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-22  |  22.3 KB  |  1,005 lines

  1. /*-
  2.  * Copyright (c) 1991, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)encrypt.c    8.2 (Berkeley) 5/30/95";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * Copyright (C) 1990 by the Massachusetts Institute of Technology
  40.  *
  41.  * Export of this software from the United States of America is assumed
  42.  * to require a specific license from the United States Government.
  43.  * It is the responsibility of any person or organization contemplating
  44.  * export to obtain such a license before exporting.
  45.  *
  46.  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  47.  * distribute this software and its documentation for any purpose and
  48.  * without fee is hereby granted, provided that the above copyright
  49.  * notice appear in all copies and that both that copyright notice and
  50.  * this permission notice appear in supporting documentation, and that
  51.  * the name of M.I.T. not be used in advertising or publicity pertaining
  52.  * to distribution of the software without specific, written prior
  53.  * permission.  M.I.T. makes no representations about the suitability of
  54.  * this software for any purpose.  It is provided "as is" without express
  55.  * or implied warranty.
  56.  */
  57.  
  58. #ifdef HAVE_CONFIG_H
  59. #include <config.h>
  60. #endif
  61.  
  62. #ifdef    ENCRYPTION
  63.  
  64. #define    ENCRYPT_NAMES
  65. #include <arpa/telnet.h>
  66.  
  67. #include "encrypt.h"
  68. #include "misc.h"
  69.  
  70. #ifdef    __STDC__
  71. #include <stdlib.h>
  72. #endif
  73. #ifdef    NO_STRING_H
  74. #include <strings.h>
  75. #else
  76. #include <string.h>
  77. #endif
  78.  
  79. /*
  80.  * These functions pointers point to the current routines
  81.  * for encrypting and decrypting data.
  82.  */
  83. void    (*encrypt_output) P((unsigned char *, int));
  84. int    (*decrypt_input) P((int));
  85.  
  86. int encrypt_debug_mode = 0;
  87. static int decrypt_mode = 0;
  88. static int encrypt_mode = 0;
  89. static int encrypt_verbose = 0;
  90. static int autoencrypt = 0;
  91. static int autodecrypt = 0;
  92. static int havesessionkey = 0;
  93. static int Server = 0;
  94. static char *Name = "Noname";
  95.  
  96. #define    typemask(x)    ((x) > 0 ? 1 << ((x)-1) : 0)
  97.  
  98. static long i_support_encrypt = typemask(ENCTYPE_DES_CFB64)
  99.                 | typemask(ENCTYPE_DES_OFB64);
  100. static long i_support_decrypt = typemask(ENCTYPE_DES_CFB64)
  101.                 | typemask(ENCTYPE_DES_OFB64);
  102. static long i_wont_support_encrypt = 0;
  103. static long i_wont_support_decrypt = 0;
  104. #define    I_SUPPORT_ENCRYPT    (i_support_encrypt & ~i_wont_support_encrypt)
  105. #define    I_SUPPORT_DECRYPT    (i_support_decrypt & ~i_wont_support_decrypt)
  106.  
  107. static long remote_supports_encrypt = 0;
  108. static long remote_supports_decrypt = 0;
  109.  
  110. static Encryptions encryptions[] = {
  111. #ifdef    DES_ENCRYPTION
  112.     { "DES_CFB64",    ENCTYPE_DES_CFB64,
  113.             cfb64_encrypt,    
  114.             cfb64_decrypt,
  115.             cfb64_init,
  116.             cfb64_start,
  117.             cfb64_is,
  118.             cfb64_reply,
  119.             cfb64_session,
  120.             cfb64_keyid,
  121.             cfb64_printsub },
  122.     { "DES_OFB64",    ENCTYPE_DES_OFB64,
  123.             ofb64_encrypt,    
  124.             ofb64_decrypt,
  125.             ofb64_init,
  126.             ofb64_start,
  127.             ofb64_is,
  128.             ofb64_reply,
  129.             ofb64_session,
  130.             ofb64_keyid,
  131.             ofb64_printsub },
  132. #endif    /* DES_ENCRYPTION */
  133.     { 0, },
  134. };
  135.  
  136. static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPT,
  137.                      ENCRYPT_SUPPORT };
  138. static unsigned char str_suplen = 0;
  139. static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPT };
  140. static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPT, 0, IAC, SE };
  141.  
  142.     Encryptions *
  143. findencryption(type)
  144.     int type;
  145. {
  146.     Encryptions *ep = encryptions;
  147.  
  148.     if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & typemask(type)))
  149.         return(0);
  150.     while (ep->type && ep->type != type)
  151.         ++ep;
  152.     return(ep->type ? ep : 0);
  153. }
  154.  
  155.     Encryptions *
  156. finddecryption(type)
  157.     int type;
  158. {
  159.     Encryptions *ep = encryptions;
  160.  
  161.     if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & typemask(type)))
  162.         return(0);
  163.     while (ep->type && ep->type != type)
  164.         ++ep;
  165.     return(ep->type ? ep : 0);
  166. }
  167.  
  168. #define    MAXKEYLEN 64
  169.  
  170. static struct key_info {
  171.     unsigned char keyid[MAXKEYLEN];
  172.     int keylen;
  173.     int dir;
  174.     int *modep;
  175.     Encryptions *(*getcrypt)();
  176. } ki[2] = {
  177.     { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
  178.     { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
  179. };
  180.  
  181.     void
  182. encrypt_init(name, server)
  183.     char *name;
  184.     int server;
  185. {
  186.     Encryptions *ep = encryptions;
  187.  
  188.     Name = name;
  189.     Server = server;
  190.     i_support_encrypt = i_support_decrypt = 0;
  191.     remote_supports_encrypt = remote_supports_decrypt = 0;
  192.     encrypt_mode = 0;
  193.     decrypt_mode = 0;
  194.     encrypt_output = 0;
  195.     decrypt_input = 0;
  196. #ifdef notdef
  197.     encrypt_verbose = !server;
  198. #endif
  199.  
  200.     str_suplen = 4;
  201.  
  202.     while (ep->type) {
  203.         if (encrypt_debug_mode)
  204.             printf(">>>%s: I will support %s\r\n",
  205.                 Name, ENCTYPE_NAME(ep->type));
  206.         i_support_encrypt |= typemask(ep->type);
  207.         i_support_decrypt |= typemask(ep->type);
  208.         if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
  209.             if ((str_send[str_suplen++] = ep->type) == IAC)
  210.                 str_send[str_suplen++] = IAC;
  211.         if (ep->init)
  212.             (*ep->init)(Server);
  213.         ++ep;
  214.     }
  215.     str_send[str_suplen++] = IAC;
  216.     str_send[str_suplen++] = SE;
  217. }
  218.  
  219.     void
  220. encrypt_list_types()
  221. {
  222.     Encryptions *ep = encryptions;
  223.  
  224.     printf("Valid encryption types:\n");
  225.     while (ep->type) {
  226.         printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep->type), ep->type);
  227.         ++ep;
  228.     }
  229. }
  230.  
  231.     int
  232. EncryptEnable(type, mode)
  233.     char *type, *mode;
  234. {
  235.     if (isprefix(type, "help") || isprefix(type, "?")) {
  236.         printf("Usage: encrypt enable <type> [input|output]\n");
  237.         encrypt_list_types();
  238.         return(0);
  239.     }
  240.     if (EncryptType(type, mode))
  241.         return(EncryptStart(mode));
  242.     return(0);
  243. }
  244.  
  245.     int
  246. EncryptDisable(type, mode)
  247.     char *type, *mode;
  248. {
  249.     register Encryptions *ep;
  250.     int ret = 0;
  251.  
  252.     if (isprefix(type, "help") || isprefix(type, "?")) {
  253.         printf("Usage: encrypt disable <type> [input|output]\n");
  254.         encrypt_list_types();
  255.     } else if ((ep = (Encryptions *)genget(type, encryptions,
  256.                         sizeof(Encryptions))) == 0) {
  257.         printf("%s: invalid encryption type\n", type);
  258.     } else if (Ambiguous(ep)) {
  259.         printf("Ambiguous type '%s'\n", type);
  260.     } else {
  261.         if ((mode == 0) || (isprefix(mode, "input") ? 1 : 0)) {
  262.             if (decrypt_mode == ep->type)
  263.                 EncryptStopInput();
  264.             i_wont_support_decrypt |= typemask(ep->type);
  265.             ret = 1;
  266.         }
  267.         if ((mode == 0) || (isprefix(mode, "output"))) {
  268.             if (encrypt_mode == ep->type)
  269.                 EncryptStopOutput();
  270.             i_wont_support_encrypt |= typemask(ep->type);
  271.             ret = 1;
  272.         }
  273.         if (ret == 0)
  274.             printf("%s: invalid encryption mode\n", mode);
  275.     }
  276.     return(ret);
  277. }
  278.  
  279.     int
  280. EncryptType(type, mode)
  281.     char *type;
  282.     char *mode;
  283. {
  284.     register Encryptions *ep;
  285.     int ret = 0;
  286.  
  287.     if (isprefix(type, "help") || isprefix(type, "?")) {
  288.         printf("Usage: encrypt type <type> [input|output]\n");
  289.         encrypt_list_types();
  290.     } else if ((ep = (Encryptions *)genget(type, encryptions,
  291.                         sizeof(Encryptions))) == 0) {
  292.         printf("%s: invalid encryption type\n", type);
  293.     } else if (Ambiguous(ep)) {
  294.         printf("Ambiguous type '%s'\n", type);
  295.     } else {
  296.         if ((mode == 0) || isprefix(mode, "input")) {
  297.             decrypt_mode = ep->type;
  298.             i_wont_support_decrypt &= ~typemask(ep->type);
  299.             ret = 1;
  300.         }
  301.         if ((mode == 0) || isprefix(mode, "output")) {
  302.             encrypt_mode = ep->type;
  303.             i_wont_support_encrypt &= ~typemask(ep->type);
  304.             ret = 1;
  305.         }
  306.         if (ret == 0)
  307.             printf("%s: invalid encryption mode\n", mode);
  308.     }
  309.     return(ret);
  310. }
  311.  
  312.     int
  313. EncryptStart(mode)
  314.     char *mode;
  315. {
  316.     register int ret = 0;
  317.     if (mode) {
  318.         if (isprefix(mode, "input"))
  319.             return(EncryptStartInput());
  320.         if (isprefix(mode, "output"))
  321.             return(EncryptStartOutput());
  322.         if (isprefix(mode, "help") || isprefix(mode, "?")) {
  323.             printf("Usage: encrypt start [input|output]\n");
  324.             return(0);
  325.         }
  326.         printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode);
  327.         return(0);
  328.     }
  329.     ret += EncryptStartInput();
  330.     ret += EncryptStartOutput();
  331.     return(ret);
  332. }
  333.  
  334.     int
  335. EncryptStartInput()
  336. {
  337.     if (decrypt_mode) {
  338.         encrypt_send_request_start();
  339.         return(1);
  340.     }
  341.     printf("No previous decryption mode, decryption not enabled\r\n");
  342.     return(0);
  343. }
  344.  
  345.     int
  346. EncryptStartOutput()
  347. {
  348.     if (encrypt_mode) {
  349.         encrypt_start_output(encrypt_mode);
  350.         return(1);
  351.     }
  352.     printf("No previous encryption mode, encryption not enabled\r\n");
  353.     return(0);
  354. }
  355.  
  356.     int
  357. EncryptStop(mode)
  358.     char *mode;
  359. {
  360.     int ret = 0;
  361.     if (mode) {
  362.         if (isprefix(mode, "input"))
  363.             return(EncryptStopInput());
  364.         if (isprefix(mode, "output"))
  365.             return(EncryptStopOutput());
  366.         if (isprefix(mode, "help") || isprefix(mode, "?")) {
  367.             printf("Usage: encrypt stop [input|output]\n");
  368.             return(0);
  369.         }
  370.         printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode);
  371.         return(0);
  372.     }
  373.     ret += EncryptStopInput();
  374.     ret += EncryptStopOutput();
  375.     return(ret);
  376. }
  377.  
  378.     int
  379. EncryptStopInput()
  380. {
  381.     encrypt_send_request_end();
  382.     return(1);
  383. }
  384.  
  385.     int
  386. EncryptStopOutput()
  387. {
  388.     encrypt_send_end();
  389.     return(1);
  390. }
  391.  
  392.     void
  393. encrypt_display()
  394. {
  395.     if (encrypt_output)
  396.         printf("Currently encrypting output with %s\r\n",
  397.             ENCTYPE_NAME(encrypt_mode));
  398.     if (decrypt_input)
  399.         printf("Currently decrypting input with %s\r\n",
  400.             ENCTYPE_NAME(decrypt_mode));
  401. }
  402.  
  403.     int
  404. EncryptStatus()
  405. {
  406.     if (encrypt_output)
  407.         printf("Currently encrypting output with %s\r\n",
  408.             ENCTYPE_NAME(encrypt_mode));
  409.     else if (encrypt_mode) {
  410.         printf("Currently output is clear text.\r\n");
  411.         printf("Last encryption mode was %s\r\n",
  412.             ENCTYPE_NAME(encrypt_mode));
  413.     }
  414.     if (decrypt_input) {
  415.         printf("Currently decrypting input with %s\r\n",
  416.             ENCTYPE_NAME(decrypt_mode));
  417.     } else if (decrypt_mode) {
  418.         printf("Currently input is clear text.\r\n");
  419.         printf("Last decryption mode was %s\r\n",
  420.             ENCTYPE_NAME(decrypt_mode));
  421.     }
  422.     return 1;
  423. }
  424.  
  425.     void
  426. encrypt_send_support()
  427. {
  428.     if (str_suplen) {
  429.         /*
  430.          * If the user has requested that decryption start
  431.          * immediatly, then send a "REQUEST START" before
  432.          * we negotiate the type.
  433.          */
  434.         if (!Server && autodecrypt)
  435.             encrypt_send_request_start();
  436.         net_write(str_send, str_suplen);
  437.         printsub('>', &str_send[2], str_suplen - 2);
  438.         str_suplen = 0;
  439.     }
  440. }
  441.  
  442.     int
  443. EncryptDebug(on)
  444.     int on;
  445. {
  446.     if (on < 0)
  447.         encrypt_debug_mode ^= 1;
  448.     else
  449.         encrypt_debug_mode = on;
  450.     printf("Encryption debugging %s\r\n",
  451.         encrypt_debug_mode ? "enabled" : "disabled");
  452.     return(1);
  453. }
  454.  
  455.     int
  456. EncryptVerbose(on)
  457.     int on;
  458. {
  459.     if (on < 0)
  460.         encrypt_verbose ^= 1;
  461.     else
  462.         encrypt_verbose = on;
  463.     printf("Encryption %s verbose\r\n",
  464.         encrypt_verbose ? "is" : "is not");
  465.     return(1);
  466. }
  467.  
  468.     int
  469. EncryptAutoEnc(on)
  470.     int on;
  471. {
  472.     encrypt_auto(on);
  473.     printf("Automatic encryption of output is %s\r\n",
  474.         autoencrypt ? "enabled" : "disabled");
  475.     return(1);
  476. }
  477.  
  478.     int
  479. EncryptAutoDec(on)
  480.     int on;
  481. {
  482.     decrypt_auto(on);
  483.     printf("Automatic decryption of input is %s\r\n",
  484.         autodecrypt ? "enabled" : "disabled");
  485.     return(1);
  486. }
  487.  
  488. /*
  489.  * Called when ENCRYPT SUPPORT is received.
  490.  */
  491.     void
  492. encrypt_support(typelist, cnt)
  493.     unsigned char *typelist;
  494.     int cnt;
  495. {
  496.     register int type, use_type = 0;
  497.     Encryptions *ep;
  498.  
  499.     /*
  500.      * Forget anything the other side has previously told us.
  501.      */
  502.     remote_supports_decrypt = 0;
  503.  
  504.     while (cnt-- > 0) {
  505.         type = *typelist++;
  506.         if (encrypt_debug_mode)
  507.             printf(">>>%s: He is supporting %s (%d)\r\n",
  508.                 Name,
  509.                 ENCTYPE_NAME(type), type);
  510.         if ((type < ENCTYPE_CNT) &&
  511.             (I_SUPPORT_ENCRYPT & typemask(type))) {
  512.             remote_supports_decrypt |= typemask(type);
  513.             if (use_type == 0)
  514.                 use_type = type;
  515.         }
  516.     }
  517.     if (use_type) {
  518.         ep = findencryption(use_type);
  519.         if (!ep)
  520.             return;
  521.         type = ep->start ? (*ep->start)(DIR_ENCRYPT, Server) : 0;
  522.         if (encrypt_debug_mode)
  523.             printf(">>>%s: (*ep->start)() returned %d\r\n",
  524.                     Name, type);
  525.         if (type < 0)
  526.             return;
  527.         encrypt_mode = use_type;
  528.         if (type == 0)
  529.             encrypt_start_output(use_type);
  530.     }
  531. }
  532.  
  533.     void
  534. encrypt_is(data, cnt)
  535.     unsigned char *data;
  536.     int cnt;
  537. {
  538.     Encryptions *ep;
  539.     register int type, ret;
  540.  
  541.     if (--cnt < 0)
  542.         return;
  543.     type = *data++;
  544.     if (type < ENCTYPE_CNT)
  545.         remote_supports_encrypt |= typemask(type);
  546.     if (!(ep = finddecryption(type))) {
  547.         if (encrypt_debug_mode)
  548.             printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
  549.                 Name,
  550.                 ENCTYPE_NAME_OK(type)
  551.                     ? ENCTYPE_NAME(type) : "(unknown)",
  552.                 type);
  553.         return;
  554.     }
  555.     if (!ep->is) {
  556.         if (encrypt_debug_mode)
  557.             printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
  558.                 Name,
  559.                 ENCTYPE_NAME_OK(type)
  560.                     ? ENCTYPE_NAME(type) : "(unknown)",
  561.                 type);
  562.         ret = 0;
  563.     } else {
  564.         ret = (*ep->is)(data, cnt);
  565.         if (encrypt_debug_mode)
  566.             printf("(*ep->is)(%x, %d) returned %s(%d)\n", data, cnt,
  567.                 (ret < 0) ? "FAIL " :
  568.                 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
  569.     }
  570.     if (ret < 0) {
  571.         autodecrypt = 0;
  572.     } else {
  573.         decrypt_mode = type;
  574.         if (ret == 0 && autodecrypt)
  575.             encrypt_send_request_start();
  576.     }
  577. }
  578.  
  579.     void
  580. encrypt_reply(data, cnt)
  581.     unsigned char *data;
  582.     int cnt;
  583. {
  584.     Encryptions *ep;
  585.     register int ret, type;
  586.  
  587.     if (--cnt < 0)
  588.         return;
  589.     type = *data++;
  590.     if (!(ep = findencryption(type))) {
  591.         if (encrypt_debug_mode)
  592.             printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
  593.                 Name,
  594.                 ENCTYPE_NAME_OK(type)
  595.                     ? ENCTYPE_NAME(type) : "(unknown)",
  596.                 type);
  597.         return;
  598.     }
  599.     if (!ep->reply) {
  600.         if (encrypt_debug_mode)
  601.             printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
  602.                 Name,
  603.                 ENCTYPE_NAME_OK(type)
  604.                     ? ENCTYPE_NAME(type) : "(unknown)",
  605.                 type);
  606.         ret = 0;
  607.     } else {
  608.         ret = (*ep->reply)(data, cnt);
  609.         if (encrypt_debug_mode)
  610.             printf("(*ep->reply)(%x, %d) returned %s(%d)\n",
  611.                 data, cnt,
  612.                 (ret < 0) ? "FAIL " :
  613.                 (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret);
  614.     }
  615.     if (encrypt_debug_mode)
  616.         printf(">>>%s: encrypt_reply returned %d\n", Name, ret);
  617.     if (ret < 0) {
  618.         autoencrypt = 0;
  619.     } else {
  620.         encrypt_mode = type;
  621.         if (ret == 0 && autoencrypt)
  622.             encrypt_start_output(type);
  623.     }
  624. }
  625.  
  626. /*
  627.  * Called when a ENCRYPT START command is received.
  628.  */
  629.     void
  630. encrypt_start(data, cnt)
  631.     unsigned char *data;
  632.     int cnt;
  633. {
  634.     Encryptions *ep;
  635.  
  636.     if (!decrypt_mode) {
  637.         /*
  638.          * Something is wrong.  We should not get a START
  639.          * command without having already picked our
  640.          * decryption scheme.  Send a REQUEST-END to
  641.          * attempt to clear the channel...
  642.          */
  643.         printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name);
  644.         encrypt_send_request_end();
  645.         return;
  646.     }
  647.  
  648.     if (ep = finddecryption(decrypt_mode)) {
  649.         decrypt_input = ep->input;
  650.         if (encrypt_verbose)
  651.             printf("[ Input is now decrypted with type %s ]\r\n",
  652.                 ENCTYPE_NAME(decrypt_mode));
  653.         if (encrypt_debug_mode)
  654.             printf(">>>%s: Start to decrypt input with type %s\r\n",
  655.                 Name, ENCTYPE_NAME(decrypt_mode));
  656.     } else {
  657.         printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
  658.                 Name,
  659.                 ENCTYPE_NAME_OK(decrypt_mode)
  660.                     ? ENCTYPE_NAME(decrypt_mode)
  661.                     : "(unknown)",
  662.                 decrypt_mode);
  663.         encrypt_send_request_end();
  664.     }
  665. }
  666.  
  667.     void
  668. encrypt_session_key(key, server)
  669.     Session_Key *key;
  670.     int server;
  671. {
  672.     Encryptions *ep = encryptions;
  673.  
  674.     havesessionkey = 1;
  675.  
  676.     while (ep->type) {
  677.         if (ep->session)
  678.             (*ep->session)(key, server);
  679. #ifdef notdef
  680.         if (!encrypt_output && autoencrypt && !server)
  681.             encrypt_start_output(ep->type);
  682.         if (!decrypt_input && autodecrypt && !server)
  683.             encrypt_send_request_start();
  684. #endif
  685.         ++ep;
  686.     }
  687. }
  688.  
  689. /*
  690.  * Called when ENCRYPT END is received.
  691.  */
  692.     void
  693. encrypt_end()
  694. {
  695.     decrypt_input = 0;
  696.     if (encrypt_debug_mode)
  697.         printf(">>>%s: Input is back to clear text\r\n", Name);
  698.     if (encrypt_verbose)
  699.         printf("[ Input is now clear text ]\r\n");
  700. }
  701.  
  702. /*
  703.  * Called when ENCRYPT REQUEST-END is received.
  704.  */
  705.     void
  706. encrypt_request_end()
  707. {
  708.     encrypt_send_end();
  709. }
  710.  
  711. /*
  712.  * Called when ENCRYPT REQUEST-START is received.  If we receive
  713.  * this before a type is picked, then that indicates that the
  714.  * other side wants us to start encrypting data as soon as we
  715.  * can. 
  716.  */
  717.     void
  718. encrypt_request_start(data, cnt)
  719.     unsigned char *data;
  720.     int cnt;
  721. {
  722.     if (encrypt_mode == 0)  {
  723.         if (Server)
  724.             autoencrypt = 1;
  725.         return;
  726.     }
  727.     encrypt_start_output(encrypt_mode);
  728. }
  729.  
  730. static unsigned char str_keyid[(MAXKEYLEN*2)+5] = { IAC, SB, TELOPT_ENCRYPT };
  731.  
  732. encrypt_enc_keyid(keyid, len)
  733.     unsigned char *keyid;
  734.     int len;
  735. {
  736.     encrypt_keyid(&ki[1], keyid, len);
  737. }
  738.  
  739. encrypt_dec_keyid(keyid, len)
  740.     unsigned char *keyid;
  741.     int len;
  742. {
  743.     encrypt_keyid(&ki[0], keyid, len);
  744. }
  745.  
  746. encrypt_keyid(kp, keyid, len)
  747.     struct key_info *kp;
  748.     unsigned char *keyid;
  749.     int len;
  750. {
  751.     Encryptions *ep;
  752.     unsigned char *strp, *cp;
  753.     int dir = kp->dir;
  754.     register int ret = 0;
  755.  
  756.     if (!(ep = (*kp->getcrypt)(*kp->modep))) {
  757.         if (len == 0)
  758.             return;
  759.         kp->keylen = 0;
  760.     } else if (len == 0) {
  761.         /*
  762.          * Empty option, indicates a failure.
  763.          */
  764.         if (kp->keylen == 0)
  765.             return;
  766.         kp->keylen = 0;
  767.         if (ep->keyid)
  768.             (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
  769.  
  770.     } else if ((len != kp->keylen) ||
  771.            (memcmp(keyid, kp->keyid, len) != 0)) {
  772.         /*
  773.          * Length or contents are different
  774.          */
  775.         kp->keylen = len;
  776.         memmove(kp->keyid, keyid, len);
  777.         if (ep->keyid)
  778.             (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
  779.     } else {
  780.         if (ep->keyid)
  781.             ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
  782.         if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
  783.             encrypt_start_output(*kp->modep);
  784.         return;
  785.     }
  786.  
  787.     encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
  788. }
  789.  
  790.     void
  791. encrypt_send_keyid(dir, keyid, keylen, saveit)
  792.     int dir;
  793.     unsigned char *keyid;
  794.     int keylen;
  795.     int saveit;
  796. {
  797.     unsigned char *strp;
  798.  
  799.     str_keyid[3] = (dir == DIR_ENCRYPT)
  800.             ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
  801.     if (saveit) {
  802.         struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
  803.         memmove(kp->keyid, keyid, keylen);
  804.         kp->keylen = keylen;
  805.     }
  806.  
  807.     for (strp = &str_keyid[4]; keylen > 0; --keylen) {
  808.         if ((*strp++ = *keyid++) == IAC)
  809.             *strp++ = IAC;
  810.     }
  811.     *strp++ = IAC;
  812.     *strp++ = SE;
  813.     net_write(str_keyid, strp - str_keyid);
  814.     printsub('>', &str_keyid[2], strp - str_keyid - 2);
  815. }
  816.  
  817.     void
  818. encrypt_auto(on)
  819.     int on;
  820. {
  821.     if (on < 0)
  822.         autoencrypt ^= 1;
  823.     else
  824.         autoencrypt = on ? 1 : 0;
  825. }
  826.  
  827.     void
  828. decrypt_auto(on)
  829.     int on;
  830. {
  831.     if (on < 0)
  832.         autodecrypt ^= 1;
  833.     else
  834.         autodecrypt = on ? 1 : 0;
  835. }
  836.  
  837.     void
  838. encrypt_start_output(type)
  839.     int type;
  840. {
  841.     Encryptions *ep;
  842.     register unsigned char *p;
  843.     register int i;
  844.  
  845.     if (!(ep = findencryption(type))) {
  846.         if (encrypt_debug_mode) {
  847.             printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
  848.                 Name,
  849.                 ENCTYPE_NAME_OK(type)
  850.                     ? ENCTYPE_NAME(type) : "(unknown)",
  851.                 type);
  852.         }
  853.         return;
  854.     }
  855.     if (ep->start) {
  856.         i = (*ep->start)(DIR_ENCRYPT, Server);
  857.         if (encrypt_debug_mode) {
  858.             printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
  859.                 Name, 
  860.                 (i < 0) ? "failed" :
  861.                     "initial negotiation in progress",
  862.                 i, ENCTYPE_NAME(type));
  863.         }
  864.         if (i)
  865.             return;
  866.     }
  867.     p = str_start + 3;
  868.     *p++ = ENCRYPT_START;
  869.     for (i = 0; i < ki[0].keylen; ++i) {
  870.         if ((*p++ = ki[0].keyid[i]) == IAC)
  871.             *p++ = IAC;
  872.     }
  873.     *p++ = IAC;
  874.     *p++ = SE;
  875.     net_write(str_start, p - str_start);
  876.     net_encrypt();
  877.     printsub('>', &str_start[2], p - &str_start[2]);
  878.     /*
  879.      * If we are already encrypting in some mode, then
  880.      * encrypt the ring (which includes our request) in
  881.      * the old mode, mark it all as "clear text" and then
  882.      * switch to the new mode.
  883.      */
  884.     encrypt_output = ep->output;
  885.     encrypt_mode = type;
  886.     if (encrypt_debug_mode)
  887.         printf(">>>%s: Started to encrypt output with type %s\r\n",
  888.             Name, ENCTYPE_NAME(type));
  889.     if (encrypt_verbose)
  890.         printf("[ Output is now encrypted with type %s ]\r\n",
  891.             ENCTYPE_NAME(type));
  892. }
  893.  
  894.     void
  895. encrypt_send_end()
  896. {
  897.     if (!encrypt_output)
  898.         return;
  899.  
  900.     str_end[3] = ENCRYPT_END;
  901.     net_write(str_end, sizeof(str_end));
  902.     net_encrypt();
  903.     printsub('>', &str_end[2], sizeof(str_end) - 2);
  904.     /*
  905.      * Encrypt the output buffer now because it will not be done by
  906.      * netflush...
  907.      */
  908.     encrypt_output = 0;
  909.     if (encrypt_debug_mode)
  910.         printf(">>>%s: Output is back to clear text\r\n", Name);
  911.     if (encrypt_verbose)
  912.         printf("[ Output is now clear text ]\r\n");
  913. }
  914.  
  915.     void
  916. encrypt_send_request_start()
  917. {
  918.     register unsigned char *p;
  919.     register int i;
  920.  
  921.     p = &str_start[3];
  922.     *p++ = ENCRYPT_REQSTART;
  923.     for (i = 0; i < ki[1].keylen; ++i) {
  924.         if ((*p++ = ki[1].keyid[i]) == IAC)
  925.             *p++ = IAC;
  926.     }
  927.     *p++ = IAC;
  928.     *p++ = SE;
  929.     net_write(str_start, p - str_start);
  930.     printsub('>', &str_start[2], p - &str_start[2]);
  931.     if (encrypt_debug_mode)
  932.         printf(">>>%s: Request input to be encrypted\r\n", Name);
  933. }
  934.  
  935.     void
  936. encrypt_send_request_end()
  937. {
  938.     str_end[3] = ENCRYPT_REQEND;
  939.     net_write(str_end, sizeof(str_end));
  940.     printsub('>', &str_end[2], sizeof(str_end) - 2);
  941.  
  942.     if (encrypt_debug_mode)
  943.         printf(">>>%s: Request input to be clear text\r\n", Name);
  944. }
  945.  
  946.     void
  947. encrypt_wait()
  948. {
  949.     register int encrypt, decrypt;
  950.     if (encrypt_debug_mode)
  951.         printf(">>>%s: in encrypt_wait\r\n", Name);
  952.     if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt))
  953.         return;
  954.     while (autoencrypt && !encrypt_output)
  955.         if (telnet_spin())
  956.             return;
  957. }
  958.  
  959.     void
  960. encrypt_debug(mode)
  961.     int mode;
  962. {
  963.     encrypt_debug_mode = mode;
  964. }
  965.  
  966.     void
  967. encrypt_gen_printsub(data, cnt, buf, buflen)
  968.     unsigned char *data, *buf;
  969.     int cnt, buflen;
  970. {
  971.     char tbuf[16], *cp;
  972.  
  973.     cnt -= 2;
  974.     data += 2;
  975.     buf[buflen-1] = '\0';
  976.     buf[buflen-2] = '*';
  977.     buflen -= 2;;
  978.     for (; cnt > 0; cnt--, data++) {
  979.         sprintf(tbuf, " %d", *data);
  980.         for (cp = tbuf; *cp && buflen > 0; --buflen)
  981.             *buf++ = *cp++;
  982.         if (buflen <= 0)
  983.             return;
  984.     }
  985.     *buf = '\0';
  986. }
  987.  
  988.     void
  989. encrypt_printsub(data, cnt, buf, buflen)
  990.     unsigned char *data, *buf;
  991.     int cnt, buflen;
  992. {
  993.     Encryptions *ep;
  994.     register int type = data[1];
  995.  
  996.     for (ep = encryptions; ep->type && ep->type != type; ep++)
  997.         ;
  998.  
  999.     if (ep->printsub)
  1000.         (*ep->printsub)(data, cnt, buf, buflen);
  1001.     else
  1002.         encrypt_gen_printsub(data, cnt, buf, buflen);
  1003. }
  1004. #endif    /* ENCRYPTION */
  1005.